package com.tencent.servlet;

import com.tencent.myssm.ViewBaseServlet;
import com.tencent.pojo.Student;
import com.tencent.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author: Fame
 * @date: 2022-06-23 11:11
 * @description:
 */
@WebServlet("/search.do")
public class SearchServlet extends ViewBaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        int pageNo = 1;
        String keyword = req.getParameter("keyword");
        String oper = req.getParameter("oper");
        HttpSession session1 = req.getSession();
        if (oper != null && "search".equals(oper)) {
            pageNo = 1;
            if (keyword == null){
                keyword = "";
            }
            session1.setAttribute("keyword",keyword);
        } else {
            String pageStr = req.getParameter("pageNo");
            if (pageStr != null && pageStr != "") {
                pageNo = Integer.valueOf(pageStr);
            }
            Object keyword1 = session1.getAttribute("keyword");
            if (keyword1 != null){
                keyword = (String) keyword1;
            }else keyword ="";
        }



        session1.setAttribute("pageNo", pageNo);
        StudentServiceImpl service = new StudentServiceImpl();
        // 总记录条数
        int count = service.getStuCount(keyword);
        // 总记录页数
        int pageEnd = (count + 5 - 1) / 5;

        List<Student> list = service.get(keyword,pageNo);//查询到list数据
        HttpSession session = req.getSession(); //获取session会话
        session.setAttribute("studentList", list);//设置Attribute
        session.setAttribute("pageEnd", pageEnd);
        super.processTemplate("student", req, resp);//重定向到父类
    }
}
